import 'dart:async';
import 'package:second_hand/API/file/file.dart';
import 'dart:io';
import '../../data/shared_preferences_service.dart';
import '../request_ways.dart';
class ApiService {

  // 登录接口
  static Future<Map<String, dynamic>> login(String phone, String password) {
    return Request.postRequest(
      path: "/user/login",
      body: {"phone": phone, "password": password},
    );
  }

  // 发送验证码接口
  static Future<Map<String, dynamic>> sendCode(String phone) {
    return Request.postRequest(path: "/sms/send", body: {"phone": phone});
  }

  // 注册接口
  static Future<Map<String, dynamic>> register(
    String phone,
    String name,
    String password,
    String studentId,
    String campus,
    String code,
  ) {
    return Request.postRequest(
      path: "/user/register",
      body: {
        "phone": phone,
        "name": name,
        "password": password,
        "student_id": studentId,
        "campus": campus,
        "code": code,
      },
    );
  }

  //获取用户信息接口
  static Future<Map<String, dynamic>> getUserInfo(String token) async {
    return Request.getRequest(
      path: "/user/info?timestamp=${DateTime.now().millisecondsSinceEpoch}", // 防止缓存
      header: {"Authorization": token},
    );
  }

  // 更新用户头像接口
  static Future<Map<String, dynamic>> updateAvatar(File imageFile) async {
    String? token = await SharedPreferencesService.getToken();
    if (token == null || token.isEmpty) {
      return {"code": 40020, "message": "未授权，请先登录", "data": null};
    }
    final response = await FileService.uploadFile(imageFile, token);
    if (response['code'] == 40021) {
      return Request.patchRequest(
        path: "/user/update_picture",
        queryParams: {"url": response['data']},
        header: {'Authorization':token}
      );
    } else {
      return response;
    }
  }

  // 修改用户信息接口（昵称/校区）
  static Future<Map<String, dynamic>> updateUserInfo({
    required String name,
    required String campus,
  }) async {
    String? token = await SharedPreferencesService.getToken();
    if (token == null || token.isEmpty) {
      return {"code": 40020, "message": "未授权，请先登录", "data": null};
    }
    return Request.putRequest(
      path: "/user/update_info",
      body: {
        "name": name,
        "campus": campus,
      },
      headers: {'Authorization': token},
    );
  }

  //忘记密码接口
  static Future<Map<String, dynamic>> forgetPassword({
    required String phone,
    required String password,
    required String code,
  }) async {
    return Request.patchRequest(
      path: "/user/forget_password",
      body: {
        "phone": phone,
        "new_password": password,
        "re_password": password,
        "code": code,
      },
      header: {'Content-Type': 'application/json'}, // 显式设置 Content-Type
    );
  }

  //修改密码接口
  static Future<Map<String, dynamic>> updatePassword({
    required String oldPassword,
    required String newPassword,
    required String rePassword,
  }) async {
    String? token= await SharedPreferencesService.getToken();
    if (token == null || token.isEmpty) {
      return {"code": 40020, "message": "未授权，请先登录", "data": null};
    }
    return Request.patchRequest(
      path: "/user/update_password",
      body: {
        "old_password": oldPassword,
        "new_password": newPassword,
        "re_password": rePassword,
      },
      header: {'Content-Type': 'application/json','Authorization':token}, // 显式设置 Content-Type
    );
  }
}
